from src.ifc.property.property import add_property
from src.ifc.rule.beam import beam_task
from src.ifc.rule.clean import *
from src.ifc.rule.column import *
from src.ifc.rule.disassemble_board import *
from src.ifc.rule.mix import (
    beam_slab_boarding_follow_column_boarding,
    maintain,
    column_concrete_follow_beam_slab_boarding,
)
from src.ifc.rule.slab import slab_task
from src.util.applog import AppLog


# 测试使用
def test_func():
    graph = Neo4jInstance.get_connect()
    # 删除所有 DEPENDENCE 关系，测试使用，不需要重新执行导入
    graph.run(
        """
    MATCH (s)-[r:DEPENDENCE]-(n)
    DELETE r
    """
    )
    graph.run(
        """
    MATCH (s)-[r:SUBITEM]-(n)
    DELETE r
    """
    )
    graph.run(
        """
    MATCH (s)-[r:RELATED]-(n)
    DELETE r
    """
    )

    graph.run("MATCH (n:Virtual) DELETE n")

    graph.run("MATCH (n:VirtualColumnSteel) DELETE n")
    graph.run("MATCH (n:VirtualColumnBoard) DELETE n")
    graph.run("MATCH (n:TaskColumnConcrete) DELETE n")

    graph.run("MATCH (n:VirtualBeamSlaSteel) DELETE n")
    graph.run("MATCH (n:VirtualBeamSlaBoard) DELETE n")
    graph.run("MATCH (n:VirtualBeamSlaConcrete) DELETE n")

    graph.run("MATCH (n:TaskMaintain) DELETE n")
    graph.run("MATCH (n:TaskMaintainItem) DELETE n")


# 规则调用部分
def custom_rule():
    # test_func()
    # AppLog.info("删除目前不需要的节点")
    # clean_not_need_type()  # 目前只保留主体结构，测试过程

    # 属性补充
    AppLog.info("属性补充")
    # 各种属性设置
    add_property()  # 楼层顺序属性设置

    AppLog.info("楼层子构件排序")

    # 如果水泥型号一样，那就是另外的排序 # storey_container_order()  # 楼层子构件排序

    # 楼层
    storeys = get_storeys()

    # 柱任务
    AppLog.info("柱任务")
    column_task(storeys)
    # 柱任务
    AppLog.info("梁任务")
    beam_task(storeys)
    # 楼板任务
    AppLog.info("楼板任务")
    slab_task(storeys)

    # 跨组件工序
    # 梁、楼板模板安装 FOLLOW 柱模板安装
    AppLog.info("梁、楼板模板安装 FOLLOW 柱模板安装")
    beam_slab_boarding_follow_column_boarding(storeys)

    # 柱水泥浇灌 FOLLOW 梁、楼板模板
    AppLog.info("柱水泥浇灌")
    column_concrete_follow_beam_slab_boarding(storeys)

    # 下个楼层开工，依赖前一个楼层的养护
    AppLog.info("下个楼层开工，依赖前一个楼层的养护")
    maintain(storeys)

    AppLog.info("模板拆解")

    # calc_workload()


# TODO 下面的规则未实现
# 对于钢筋绑扎，梁、楼板等水平构件的钢筋默认依次绑扎
#
# 对于混凝土浇筑，同一施工层段内，柱等竖向构件默认一起浇筑，梁、楼板等水平构件默认一起浇筑。


def get_storeys() -> []:
    graph = Neo4jInstance.get_connect()
    storeys = graph.run(
        "MATCH (s:IfcBuildingStorey) RETURN s ORDER BY s.SortIndex"
    ).data()
    storeys = [storey["s"] for storey in storeys]
    return storeys


# 打印测试
def print_dependence():
    # 查询具有 DEPENDENCE 关系的节点
    query = """
    MATCH (n)-[:DEPENDENCE]->(m)
    RETURN n, m
    """

    # 执行查询
    result = graph.run(query).data()
    # 处理结果
    for record in result:
        n = record["n"]
        m = record["m"]
        print(f"Start Node: {n}, End Node: {m}")
    print(f" dependence count : {len(result)}")
